#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        vector<vector<int>> buy(n, vector<int>(3));
        auto sold = buy;
        buy[0][0] = -prices[0];
        buy[0][1] = buy[0][2] = INT_MIN;
        for (int i = 1; i < n; i++)
        {
            buy[i][0] = max(buy[i - 1][0], -prices[i]);
            sold[i][1] = max(sold[i - 1][1], buy[i - 1][0] + prices[i]);
            buy[i][1] = max(buy[i - 1][1], sold[i - 1][1] - prices[i]);
            sold[i][2] = max(sold[i - 1][2], buy[i - 1][1] + prices[i]);
        }
        return max(sold[n - 1][1], sold[n - 1][2]);
    }
};